<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/23
 * Time: 19:24
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\Invest;
use common\models\DAO\UserInvest;

class InvestService {
    public static function get($id, $userID){
        $userInvest = UserInvest::findOne(['id'=>$id, 'userID'=>$userID]);
        if($userInvest){
            $userInvest = $userInvest->attributes;
            return $userInvest;
        }else
            return false;
    }

    /*
     * 投资总额
     */
    public static function total($userID){
        $money['money'] = 0;
        $money['accrual'] = 0;
        $total = UserInvest::find()->where(['userID'=>$userID])->select('SUM(money) as money, SUM(accrual) as accrual')->one();
        if($total){
            $total = $total->attributes;
            $money['money'] = $total['money'];
            $money['accrual'] = $total['accrual'];
        }
        return $money;
    }

    /*
     * 预计收益 本金
     */
    public static function preInvest($userID){
        $money['money'] = 0;
        $money['accrual'] = 0;
        $total = UserInvest::find()->where(['userID'=>$userID, 'status'=>1])->select('SUM(money) as money, SUM(accrual) as accrual')->one();
        if($total){
            $total = $total->attributes;
            $money['money'] = intval($total['money']);
            $money['accrual'] = intval($total['accrual']);
        }
        return $money;
    }
	
	
    /*
     * 所有散标列表
     */
    public static function getList($data){
        $now = time();
        $result = [];
        $limit = 10;
        $start = (intval($data['page']) - 1) * $limit;
		//查询数据
		$investModel=Invest::find()->where("status in (1,2,3) and beginTime<=".time());
		if($data['typeID']) $investModel=$investModel->andWhere("typeID={$data['typeID']}");
		$investModel=$investModel->andWhere("investDate>{$data['begin']}");
		if($data['end']) $investModel=$investModel->andWhere("investDate<={$data['end']}");
		//获取总数
		$total=$investModel->count();
		
		$investModel=$investModel->orderBy($data['order'])->offset($start)->limit($limit)->all();
		
        if($investModel){
            foreach($investModel as $v){
                $info = $v->attributes;
                $money = intval($info['money']);
                $progress = intval($info['progress']);
                $list['person'] = $info['person'];
                $list['id'] = $info['id'];
                $list['name'] = $info['investName'];
                $list['rate'] = CommonFunction::formatMoney($info['rate']);
                $list['money'] = CommonFunction::formatWan($money - $progress);
                $list['progress'] = intval($progress/$money * 100);
                $list['day'] = $info['investDate'];
				$list['status'] = $info['status'];
                $result[] = $list;
            }
        }
		$result['total']=$total;
        return $result;
    }

    /*
    * 所有散标列表
    */
    public static function wapList($page = 1, $limit = 10){
        $now = time();
        $result = [];
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $investModel = Invest::find()->where("classID=1 and status!=0 and status!=4 and beginTime<=$now")
            ->offset($start)->limit($limit)
            ->orderBy('status asc, id desc, progress asc')->all();
        if($investModel){
            foreach($investModel as $v){
                $info = $v->attributes;
                $money = intval($info['money']);
                $progress = intval($info['progress']);
                $list['person'] = $info['person'];
                $list['id'] = $info['id'];
                $list['name'] = $info['investName'];
                $list['rate'] = CommonFunction::formatMoney($info['rate']);
                $list['money'] = CommonFunction::formatWan($money - $progress);
                $list['progress'] = intval($progress/$money * 100);
                $list['day'] = $info['investDate'];
                $list['status'] = $info['status'];
                $list['url'] = \Yii::$app->urlManager->createUrl(['detail/index', 'id' => $info['id']]);
                $result[] = $list;
            }
        }
        return $result;
    }
    /*
     * 列表初始化
     */
    public static function init($params=[]){
		//过滤参数
		//页码
		$data['page']=1;
		if(isset($params['page']))
		{
			if($params['page']<1) $params['page']=1;
			$data['page']=$params['page'];
		}
		//类别
		$data['typeID']=0;
		if(isset($params['classID'])) $data['typeID']=intval($params['classID']);
		//期限
		$data['begin']=0;
		$data['end']=0;
		if(isset($params['beginEnd']))
		{
			switch(intval($params['beginEnd']))
			{
				case '1':
					$data['begin']=0;
					$data['end']=3;
					break;
				case '2':
					$data['begin']=3;
					$data['end']=6;
					break;
				case '3':
					$data['begin']=6;
					$data['end']=12;
					break;
				case '4':
					$data['begin']=12;
					$data['end']=0;
					break;
				default:
					$data['begin']=0;
					$data['end']=0;
					break;
			}
		}
		if($data['begin']==12)
		{
			$data['begin']=365;
		}else
		{
			$data['begin']=$data['begin']*30;
		}
		if($data['end']==12)
		{
			$data['end']=365;
		}else
		{
			$data['end']=$data['end']*30;
		}
		//排序
		$data['order']='status';
		if(isset($params['order']))
		{
			switch(intval($params['order']))
			{
				case '1':
					$data['order'].=',rate';
					break;
				case '2':
					$data['order'].=',rate desc';
					break;
			}
		}
		$data['order'].=',fullTime desc,endTime';
		
        $result['list'] = self::getList($data);
		$result['total']=$result['list']['total'];
		unset($result['list']['total']);
        $result['banner'] = AdService::getBanner(3);
        return $result;
    }

    /*
     * 手机列表初始化
     */
    /*
  * 列表初始化
  */
    public static function wapInit(){
        $result['list'] = self::wapList();
        $result['banner'] = AdService::getBanner(3);
        return $result;
    }
    /*
     * 用户成功借款次数，钱数
     */
    public static function userSuccessTime($userID){
        return Invest::find()->where(['userID'=>$userID])->andWhere('status != 0 and status != 4')->select('count(*) as amount, sum(money) as money')->one_data();
    }

    /*
     * 用户还清次数
     */
    public static function userRepaymentTime($userID){
        return Invest::find()->where(['userID'=>$userID])->andWhere('status = 3')->count();
    }
}